package com.example.springbootdemo1.controller;

import com.auth0.jwt.JWT;
import com.example.springbootdemo1.entity.User;
import com.example.springbootdemo1.entity.VO.LoginUser;
import com.example.springbootdemo1.service.UserService;
import com.example.springbootdemo1.utils.JWTUtils;
import com.example.springbootdemo1.utils.PassToken;
import com.example.springbootdemo1.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @GetMapping("/test")
    public String test(){
        //读取redis中的数据
        String s = (String) redisTemplate.opsForValue().get("name");    //key的值是name
        redisTemplate.opsForValue().set("age","21",2000);
        return s;

    }
    @PostMapping("/login")
    public ServerResponse<String> login(@RequestBody LoginUser loginUser){
        //先判断验证码是否正确
        //1.如何从redis取出保存的验证码
        String storeCode = (String) redisTemplate.opsForValue().get("captcha_codes:"+loginUser.getUuid());
        System.out.println(storeCode+loginUser.toString());
        //2.用户输入的验证码和storeCode比对
        if(loginUser.getCode().equals(storeCode)){
            User u = userService.getuser(loginUser);
            if(u!=null){
                Map<String,String> map= new HashMap<>();
                map.put("userid",u.getUserId()+"");
                map.put("username",u.getUserName());
                String token = JWTUtils.getToken(map);
                redisTemplate.delete(storeCode);
                return ServerResponse.createBySuccess(200,token,"登陆成功");
            }
            return ServerResponse.createByFail(500,"用户名或密码错误");
        }else{
            return ServerResponse.createByFail(500,"验证码错误");
        }
    }
    @PassToken
    @GetMapping("/getstudent")
    public ServerResponse<String> getstudent(){
        return ServerResponse.createBySuccess(200,"","查询成功");
    }

//    @PassToken
//    @GetMapping("/getBanner")
//    public ServerResponse <List<String>> getBanner(){
//        List<String> list = new ArrayList<>();
//        list.add("/images/cluo.jpg");
//        list.add("/images/luo1.jpg");
//        list.add("/images/luo2.jpg");
//        return ServerResponse.createBySuccess(200, list,"查询成功");
//    }

    @GetMapping("/getinfo")
    public ServerResponse<User> getinfo(HttpServletRequest request){
        String token= request.getHeader("Authorization");
        System.out.println(token+"  ");
        String id = JWT.decode(token.substring(token.lastIndexOf(" ")+1)).getClaim("userid").asString();
        User user = userService.getuserById(Integer.parseInt(id));
        return ServerResponse.createBySuccess(200,user,"查询成功");
    }
}
